#include "userinfowidget.h"
#include "model/datacenter.h"
#include "mainwidget.h"

UserInfoWidget::UserInfoWidget(const model::Message& message, QWidget *parent) : message(message), QDialog(parent)
{
    //基本属性
    this->setWindowTitle("用户详情");
    this->setFixedSize(400,200);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->move(QCursor::pos());
    this->setStyleSheet("QDialog{background-color:rgb(255,255,255);}");

    //创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(20,30,20,30);
    layout->setSpacing(10);
    layout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    this->setLayout(layout);

    QSize sizeEdit(200,30); //显示区域长度

    //创建头像

        QPushButton* avatar = new QPushButton(this);
        avatar->setFixedSize(60,60);
        avatar->setIconSize(QSize(60,60));
        avatar->setStyleSheet("QPushButton{border:none;}");


    //创建序号

        QLabel* idTag = new QLabel(this);
        idTag->setText("序号");
        idTag->setStyleSheet("QLabel { font-weight: 800; }");
        QLabel* idLabel = new QLabel(this);
        idLabel->setFixedSize(sizeEdit);

    //用户昵称

        QLabel* nameTag = new QLabel(this);
        nameTag->setText("昵称");
        nameTag->setStyleSheet("QLabel { font-weight: 800; }");

        QLabel* nameLabel = new QLabel(this);
        nameLabel->setFixedSize(sizeEdit);

    //用户电话

        QLabel* phoneTag = new QLabel(this);
        phoneTag->setText("电话");
        phoneTag->setStyleSheet("QLabel { font-weight: 800; }");
        QLabel* phoneLabel = new QLabel(this);
        phoneLabel->setFixedSize(sizeEdit);


    //特殊按钮

        applyFriendBtn = new QPushButton();
        deleteFriendBtn = new QPushButton();
        sendMessageBtn = new QPushButton();

        QString btnStyle = "QPushButton { border: 1px solid rgb(90, 90, 90);border-radius: 5px; } QPushButton:pressed { background-color: rgb(235, 235,235);}";

        applyFriendBtn->setText("申请好友");
        deleteFriendBtn->setText("删除好友");
        sendMessageBtn->setText("发送消息");

        applyFriendBtn->setStyleSheet(btnStyle);
        deleteFriendBtn->setStyleSheet(btnStyle);
        sendMessageBtn->setStyleSheet(btnStyle);

        applyFriendBtn->setFixedSize(80,30);
        deleteFriendBtn->setFixedSize(80,30);
        sendMessageBtn->setFixedSize(80,30);

    //设置信息

        avatar->setIcon(message.sender.userIcon);
        idLabel->setText(message.messageId);
        nameLabel->setText(message.sender.userName);
        phoneLabel->setText(message.sender.userPhone);

    //设置布局

        //设置头像
        layout->addWidget(avatar,0,0,3,2);

        //设置文本tag和label
        layout->addWidget(idTag,0,2,1,2);
        layout->addWidget(idLabel,0,4,1,2);


        layout->addWidget(nameTag,1,2,1,2);
        layout->addWidget(nameLabel,1,4,1,2);


        layout->addWidget(phoneTag,2,2,1,2);
        layout->addWidget(phoneLabel,2,4,1,2);

        //设置特殊按钮
        layout->addWidget(applyFriendBtn, 3, 0, 1, 2);
        layout->addWidget(sendMessageBtn, 3, 2, 1, 2);
        layout->addWidget(deleteFriendBtn, 3, 4, 1, 2);

    //特殊按钮逻辑设置

        //检查对方用户是否为好友
        model::DataCenter* dataCenter = model::DataCenter::getInstance();

        model::UserInfo* user = dataCenter->findFriendById(message.sender.userId);

        if(user == nullptr)
        {
            //启用申请好友按钮
            applyFriendBtn->setEnabled(true);
            deleteFriendBtn->setEnabled(false);
            sendMessageBtn->setEnabled(false);
        }
        else
        {
            //禁用申请好友按钮
            applyFriendBtn->setEnabled(false);
            deleteFriendBtn->setEnabled(true);
            sendMessageBtn->setEnabled(true);
        }

        initSignalSlot();
}

void UserInfoWidget::initSignalSlot()
{
    //关联 发送信息逻辑
    connect(sendMessageBtn,&QPushButton::clicked,this,[=](){

        //切换到会话列表, 并切换到相应会话
        MainWidget* mainWidget = MainWidget::getInstance();
        mainWidget->switchSession(message.sender.userId);

        //关闭本窗口
        this->close();
    });

    //关联 删除好友逻辑
    connect(deleteFriendBtn,&QPushButton::clicked,this,&UserInfoWidget::clickDeleteFriendBtn);

    //关联 添加好友申请
    connect(applyFriendBtn,&QPushButton::clicked,this,&UserInfoWidget::clickApplyBtn);


}

void UserInfoWidget::clickDeleteFriendBtn()
{
    auto result = QMessageBox::warning(this,"删除好友","您确认要删除该好友吗?",QMessageBox::Ok | QMessageBox::Cancel);

    if(result != QMessageBox::Ok)
    {
        LOG() << "删除好友操作取消";
        return;
    }

    // 删除好友逻辑
    model::DataCenter* dataCenter = model::DataCenter::getInstance();

    dataCenter->deleteFriendAsync(message.sender.userId);

    //关闭本窗口
    this->close();

}

void UserInfoWidget::clickApplyBtn()
{
    // 添加好友逻辑
    model::DataCenter* dataCenter = model::DataCenter::getInstance();

    dataCenter->addFriendApplyAsync(message.sender.userId);

    //关闭本窗口
    this->close();

}





























